package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.OrderAffirm.OrderAffirm;
import com.banzhi.shop.entity.OrderManagement.Order;
import com.banzhi.shop.model.CallBack.OrdersManagementCallBack;
import com.banzhi.shop.model.IOrderManagementModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 获取订单管理
 */

public class OrderManagementImpl implements IOrderManagementModel {

    @Override
    public void getOrderData(String status, int page, final OrdersManagementCallBack callBack) {
        getOrderData(0, status, page, callBack);
    }

    @Override
    public void getOrderData(int type, String status, int page, final OrdersManagementCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.myOrdersUrl);
        params.addBodyParameter("status", status);
        params.addBodyParameter("type", type + "");
        params.addBodyParameter("page", page + "");
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("OrderManagementImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Order order = gson.fromJson(result, Order.class);
                        callBack.ongetDataSuccess(order.getRes());
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("OrderManagementImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(8)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void zhifu(String order_id, final OrdersManagementCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.payOrderInfoUrl);
        params.addBodyParameter("order_id", order_id);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("zhifu", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        OrderAffirm affirm = gson.fromJson(result, OrderAffirm.class);
                        callBack.zhifuSuccess(affirm);
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("zhifu", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(34)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void delete(String id, final OrdersManagementCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.cancelOrderUrl);
        params.addBodyParameter("id", id);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("delete", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        callBack.deleteOrderSuccess(jsonobject);
                    } else {
                        callBack.deleteOrderError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("delete", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(46)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void shouhuo(String id, final OrdersManagementCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.confirmReceiptUrl);
        params.addBodyParameter("id", id);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("shouhuo", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        callBack.querenOrderSuccess(jsonobject);
                    } else {
                        callBack.querenOrderError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("shouhuo", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(60)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
